package com.xudm.cdg.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.xudm.cdg.entity.cosmic.CosmicExcelModel;
import com.xudm.cdg.entity.cosmic.UserInteractionFunction;
import com.xudm.cdg.entity.cosmic.UserInteractiveContent;

import java.io.*;
import java.util.*;

/**
 * @author xudm
 * @Description: Word模板工具类
 */
public class WordTemplateUtil {
    public static void main(String[] args) throws IOException {

//        List<Map<String, Object>> dataList1 = new ArrayList<>();
//        List<Map<String, Object>> dataList2 = new ArrayList<>();
//        List<Map<String, Object>> dataList3 = new ArrayList<>();
//        List<Map<String, Object>> dataList4 = new ArrayList<>();
//        Map<String, Object> dataMap = new HashMap<>();
//        for (int i = 0; i < 5; i++) {
//            Map<String, Object> titleMap = new HashMap<>();
//            titleMap.put("title4", "标题4," + i + 1);
//            titleMap.put("featureDescription", "Android多角色用户点切换角色时，请求Android客户端多角色切换服务，Android端根据用户业务id查询切换用户角色信息、根据用户token获取之前登录角色信息数据、将切换的用户角色信存入Android端缓存服务、并返回Android端多角色切换服务结果信息。" + i);
//            dataList4.add(titleMap);
//        }
//
//        Map<String, Object> dataMap2 = new HashMap<>();
//        Map<String, Object> dataMap1 = new HashMap<>();
//
//        dataMap2.put("title2", "标题2");
//        dataMap2.put("dataList3", dataList3);
//        dataMap1.put("title1", "标题1");
//        dataMap1.put("dataList2", dataList2);
//
//        for (int i = 0; i < 2; i++) {
//            Map<String, Object> dataMap3 = new HashMap<>();
//            dataMap3.put("dataList4", dataList4);
//            dataMap3.put("title3", "测试二级标题xxxxx" + i);
//            dataList3.add(dataMap3);
//        }
//
//        dataList2.add(dataMap2);
//        dataList1.add(dataMap1);
//        dataMap.put("dataList1", dataList1);
//        XWPFTemplate template = XWPFTemplate.compile("D:\\DevelopSpace\\XW_Space\\workspace\\COSMIC\\template\\word_template.docx").render(dataMap);
//        template.writeAndClose(new FileOutputStream("D:\\DevelopSpace\\XW_Space\\workspace\\COSMIC\\template\\word_template_output.docx"));

        // 测试插入图片
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("title", "测试标题");
        dataMap.put("content", "测试内容");
        String umlSource = PlantumlUtil.getTestSourceString();

        //表格行循环
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        Configure config = Configure.builder().useSpringEL()
                .bind("uicList", policy).bind("uifList", policy).build();

        List<UserInteractiveContent> uicList = new ArrayList<>();
        for (int i = 0; i < 1; i++) {
            UserInteractiveContent uic = new UserInteractiveContent();
            uic.setNumber((i + 1) + "");
            uic.setType("请求");
            uic.setDesignation("请求审批节点任务提交服务");
            uic.setDescription("工单流程审批节点任务提交功能请求审批节点任务提交服务");
            uicList.add(uic);
        }

        List<UserInteractionFunction> uifList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            UserInteractionFunction uic = new UserInteractionFunction();
            uic.setNumber((i + 1) + "");
            uic.setType("请求");
            uic.setDesignation("请求审批节点任务提交服务");
            uic.setDescription("工单流程审批节点任务提交功能请求审批节点任务提交服务");
            uifList.add(uic);
        }


        InputStream inputStream = PlantumlUtil.getInputStream(umlSource);
        dataMap.put("portrait", Pictures.ofStream(inputStream, PictureType.PNG).create());
        dataMap.put("uicList", uicList);
        dataMap.put("uifList", uifList);
        XWPFTemplate template = XWPFTemplate.compile("D:\\DevelopSpace\\XW_Space\\workspace\\COSMIC\\template\\word_template_image.docx", config).render(dataMap);
        template.writeAndClose(new FileOutputStream("D:\\DevelopSpace\\XW_Space\\workspace\\COSMIC\\template\\word_template_image_output.docx"));

    }
}
